<?php
namespace App\Admin\Controller;

use App\Common\Controller\AbstractController;
use App\Server\Model\AdminGroup;
use Hyperf\Di\Annotation\Inject;
use App\Admin\Validate\GroupValidate;

class GroupController extends AbstractController
{
    /**
     * @Inject()
     * @var GroupValidate
     */
    protected $GroupValidate;
    /**
     * 权限组
     */
    public function index()
    {
        $page = $this->request->query('page');
        if ($page){
            $param = $this->request->all();
            $where = [];
            if(!empty($param['keywords'])) {
                $unity_search[] = ['title',$param['keywords']];
                $unity_search[] = ['desc',$param['keywords']];
                $where['unity_search'] = $unity_search;
            }
            $adminGroup = new AdminGroup();
            $adminGroupData = $adminGroup->getListPage($where,['*']);

            return $this->success($adminGroupData['data'], '成功', ['count'=>$adminGroupData['total']]);
        }
        else
        {
            return $this->render->render('/admin/group/index');
        }
        
    }


    public function add()
    {
        $page = $this->request->query('page');
        if ($page){
            $param = $this->request->all();

            unset($param['page']);
            $errorMessage = $this->GroupValidate->add($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }

            $data = [];
            $data['title'] = $param['title'];
            $data['desc']  = $param['desc'];
            $data['status'] = $param['status'];
            $data['rules'] = empty($param['rules'])?'':implode(',',$param['rules']);
            $data['created_at'] = date('Y-m-d H:i:s',time());

            $adminGroup = new AdminGroup();
            $addRes = $adminGroup->dataAdd($data);
            if(false == $addRes) {
                return $this->error('创建失败，请稍后再试');
            }
            return $this->success([],'创建成功');
        }
        else
        {
            return $this->render->render('/admin/group/add');
        }
        
    }


    public function edit()
    {
        $page = $this->request->query('page');
        $adminGroup = new AdminGroup();
        if ($page){
            $param = $this->request->all();

            unset($param['page']);
            $errorMessage = $this->GroupValidate->edit($this->request);
            //验证部分数据合法性
            if ($errorMessage) {
                return $this->error('提交失败：' . $errorMessage);
            }
            $data = [];
            $data['title'] = $param['title'];
            $data['desc']  = $param['desc'];
            $data['status'] = $param['status'];
            $data['id']  = $param['id'];
            $data['rules'] = empty($param['rules'])?'':implode(',',$param['rules']);

            $adminGroup = new AdminGroup();
            $addRes = $adminGroup->dataUpdate([['id','=',$param['id']]],$data);
            if(false == $addRes) {
                return $this->error('修改失败，请稍后再试');
            }
            return $this->success([],'修改成功');
        }
        else
        {
            $id = $this->request->query('id');
            if(!$id) {
                return $this->error('缺少必要条件');
            }
            $group = $adminGroup->getInfo([['id','=',$id]],['*']);
            if(empty($group)) {
                return $this->error('管理组不存在');
            }
            // 为了配合layui的tree组件存在的bug，这里将已勾选的id倒叙
            $group['rules'] = array_reverse(explode(',',$group['rules']));
            $data = [];
            $data['group'] = $group;
            return $this->render->render('/admin/group/edit', $data);
        }
        
    }


    public function delete()
    {
        $param = $this->request->all();
        $id    = $param["id"];
        if(!$id) {
            return $this->error('缺少必要条件');
        }
        $adminGroup = new AdminGroup();
        $addRes = $adminGroup->dataDelete([['id','=',$id]]);
        if ($addRes == false) {
            return $this->error('删除失败！');
        }
        return $this->success('删除成功！');
    }
}
